


/* Complier weighted event study */



use "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv", replace

cd "G:\Data\Workdata\707677\FKYY7677\Debt_relief\Statafiles\Outputs\Revision\FINAL"


keep if time==1

egen lawyer_id_unique =group(lawyerid court) // Unique lawyer id within courts. some lawyers work in multiple courts
bys lawyer_id_unique: egen n_lawyer=count(lawyer_id_unique)
egen mean_lawyer_leniency= mean(granted), by(lawyer_id_unique)

gen iv_judge2_all = (mean_lawyer_leniency*n_lawyer - granted)/(n_lawyer-1)

gen iv_lawyer2=iv_judge2_all-(iv_court-granted)/(obs_court-1)



keep if n_lawyer>=20

egen n_judge=nvals(lawyer_id_unique), by(court aar)



tab n_judge

keep if n_judge>1

keep if time==1


local Xvar_short "male age_0_40 age_41_50 age_51_60 age_61_70 erhvervsindk_mean employed_pstill_mean unemployed_pstill_mean legmarried_m1 racedum1 housedum1 passiv_m1 aktiv_m1 higher_secondary university social_mean edu_missing housing_missing timelon_dum1 timelon_dum2 timelon_dum3 timelon_dum4 house_m1"


reg granted `Xvar_short' i.aar

capture drop granted_hat_full_control
predict granted_hat_full_control,  xb 

egen propensity_full_control=xtile(granted_hat_full_control), nq(5)



keep pnr propensity_full_control iv_lawyer2 lawyer_id_unique



merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"
keep if _merge==3
drop _merge

keep if time==1
tab lawyer_id_unique, gen(lawyer_dum)



egen court_pyear =group(court pyear) 
tab court_pyear

tab time




reg granted iv_lawyer2 `Xvar_short' i.court_pyear , cluster(lawyer_id_unique)

reg iv_lawyer2 i.court_pyear if granted==0
predict lawyer_residual, res

/* Complier weights (Done like in Agan et al.(2023) QJE) */

tab propensity_full_control, gen(propdum)
gen complier_weights_5=.
foreach var of varlist propdum1-propdum5 {
	
sum lawyer_residual if `var'==1, d
local z1=r(p1)
local z99=r(p99)
reg granted lawyer_residual i.court_pyear if `var'==1
local share_`var'=_b[lawyer_residual]*(`z99'-`z1') // Share of compliers in the given decile
egen frac_`var'= mean(`var')
local stat_`var'=(`share_`var'')/frac_`var'
replace complier_weights_5=`stat_`var'' if `var'==1

// Inverse probability weights. scaling by overall complier share irrelevant. Scaling irrelevant for weights. 
	
}

keep complier_weights_5 pnr

merge 1:m pnr using "G:\Data\Workdata\707677\Common\01Data\02STATA\panel_iv"


sort pnr aar

destring pnr, replace

xtset pnr aar


/* Local macro for selection of observations */


local selection "time>-5 & time<17 & sumpre>0 & sumpost>0"




/* Create covariates for regressions */



tab aar, gen(yeardum)											/* Year dummies */

tab time if (time>-5 & time<17), gen(timedum)					/* Time dummies */



/* Create variables for difference-in-difference setup */


gen treated=granted

gen after=0

replace after=1 if time>0

gen interaction=treated*after





foreach i of num 1/21 {											/* Time dummies intearacted with treatment */
	
gen interaction`i'=timedum`i'*treated	
	
	
}




local depvar1 "erhvervsindk_real"


foreach var of varlist `depvar1'{


gen outcome=`var'


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)

gen zero_lead1=0

xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo standard



xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* [pw=complier_weights_5] if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo complier


#delimit ; 


coefplot (standard, mcolor(maroon) ciopts(color(maroon))) (complier, mcolor(blue) ciopts(color(blue))),  omitted  vertical keep(interaction1 interaction2 interaction3 zero_lead1  

interaction5 interaction6 interaction7 interaction8 interaction9 interaction10 interaction11 interaction12 interaction13 interaction14 interaction15 interaction16 interaction17 interaction18 interaction19 interaction20 interaction21)


title("`var'")



xlabel(1 "-4" 2 " " 3 "-2" 4 " " 5 "0" 6 " " 7 "2" 8 " " 9 "4" 10 " " 11 "6" 12 " " 13 "8" 

14 " " 15 "10" 16 " " 17 "12" 18 " " 19 "14" 20 " " 21 "16", angle(0) nogrid labsize(medsmall))


ylabel(0 "0" 10000 "10" 20000 "20" 30000 "30" 40000 "40",nogrid angle(0) labsize(medsmall))




yscale(titlegap(*-3))


yline(0, lcolor(black))

xline(4, lcolor(black) lpattern(dash))

ytitle("Earned Income", size(medium))

xtitle("Years", size(medsmall))

title("")


graphregion(fcolor(white)) bgcolor(white) scheme(s1color)

name(earnings, replace)


;


#delimit cr


graph export `var'_complierweight.pdf, replace

graph save `var'_complierweight, replace


drop outcome zero_lead1 pre post sumpre sumpost



}


* Employment 



local depvar1 "employed_pstill"


foreach var of varlist `depvar1'{


gen outcome=`var'


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)

gen zero_lead1=0

xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo standard



xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* [pw=complier_weights_5] if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo complier


#delimit ; 


coefplot (standard, mcolor(maroon) ciopts(color(maroon))) (complier, mcolor(blue) ciopts(color(blue))),  omitted  vertical keep(interaction1 interaction2 interaction3 zero_lead1  

interaction5 interaction6 interaction7 interaction8 interaction9 interaction10 interaction11 interaction12 interaction13 interaction14 interaction15 interaction16 interaction17 interaction18 interaction19 interaction20 interaction21)



title("`var'")




xlabel(1 "-4" 2 " " 3 "-2" 4 " " 5 "0" 6 " " 7 "2" 8 " " 9 "4" 10 " " 11 "6" 12 " " 13 "8" 

14 " " 15 "10" 16 " " 17 "12" 18 " " 19 "14" 20 " " 21 "16", angle(0) nogrid labsize(medsmall))


ylabel(0 "0%" 0.025 "2.5%" 0.05 "5.0%" 0.075 "7.5%",nogrid angle(0) labsize(medsmall))


yscale(titlegap(*-3))


yline(0, lcolor(black))

xline(4, lcolor(black) lpattern(dash))

ytitle("Employment", size(medium))

xtitle("Years", size(medsmall))

title("")


graphregion(fcolor(white)) bgcolor(white) scheme(s1color)

name(employment, replace)

;


#delimit cr


graph export `var'_complierweight.pdf, replace

graph save `var'_complierweight, replace


drop outcome zero_lead1 pre post sumpre sumpost



}


* Assets


local depvar1 "aktiv_real"


foreach var of varlist `depvar1'{


gen outcome=`var'


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)

gen zero_lead1=0

xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo standard



xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* [pw=complier_weights_5] if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo complier


#delimit ; 


coefplot (standard, mcolor(maroon) ciopts(color(maroon))) (complier, mcolor(blue) ciopts(color(blue))),  omitted  vertical keep(interaction1 interaction2 interaction3 zero_lead1  

interaction5 interaction6 interaction7 interaction8 interaction9 interaction10 interaction11 interaction12 interaction13 interaction14 interaction15 interaction16 interaction17 interaction18 interaction19 interaction20 interaction21)



title("`var'")




xlabel(1 "-4" 2 " " 3 "-2" 4 " " 5 "0" 6 " " 7 "2" 8 " " 9 "4" 10 " " 11 "6" 12 " " 13 "8" 

14 " " 15 "10" 16 " " 17 "12" 18 " " 19 "14" 20 " " 21 "16", angle(0) nogrid labsize(medsmall))


ylabel(0 "0" 100000 "100" 200000 "200" 300000 "300",nogrid angle(0) labsize(medsmall))



yscale(titlegap(*-3))


yline(0, lcolor(black))

xline(4, lcolor(black) lpattern(dash))

ytitle("Assets", size(medium))

xtitle("Years", size(medsmall))

title("")


graphregion(fcolor(white)) bgcolor(white) scheme(s1color)

name(assets, replace)

;


#delimit cr


graph export `var'_complierweight.pdf, replace

graph save `var'_complierweight, replace


drop outcome zero_lead1 pre post sumpre sumpost



}


* Wealth


local depvar1 "formue_real"


foreach var of varlist `depvar1'{


gen outcome=`var'


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)

gen zero_lead1=0

xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo standard



xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* [pw=complier_weights_5] if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo complier


#delimit ; 


coefplot (standard, mcolor(maroon) ciopts(color(maroon))) (complier, mcolor(blue) ciopts(color(blue))),  omitted  vertical keep(interaction1 interaction2 interaction3 zero_lead1  

interaction5 interaction6 interaction7 interaction8 interaction9 interaction10 interaction11 interaction12 interaction13 interaction14 interaction15 interaction16 interaction17 interaction18 interaction19 interaction20 interaction21)



title("`var'")




xlabel(1 "-4" 2 " " 3 "-2" 4 " " 5 "0" 6 " " 7 "2" 8 " " 9 "4" 10 " " 11 "6" 12 " " 13 "8" 

14 " " 15 "10" 16 " " 17 "12" 18 " " 19 "14" 20 " " 21 "16", angle(0) nogrid labsize(medsmall))



ylabel(0 "0" 100000 "100" 200000 "200" 300000 "300" 400000 "400",nogrid angle(0) labsize(medsmall))


yscale(titlegap(*-3))


yline(0, lcolor(black))

xline(4, lcolor(black) lpattern(dash))

ytitle("Wealth", size(medium))

xtitle("Years", size(medsmall))

title("")


graphregion(fcolor(white)) bgcolor(white) scheme(s1color)

name(wealth, replace)

;


#delimit cr


graph export `var'_complierweight.pdf, replace

graph save `var'_complierweight, replace


drop outcome zero_lead1 pre post sumpre sumpost



}


* Homeownership


local depvar1 "house"


foreach var of varlist `depvar1'{


gen outcome=`var'


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)

gen zero_lead1=0

xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo standard



xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* [pw=complier_weights_5] if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo complier


#delimit ; 


coefplot (standard, mcolor(maroon) ciopts(color(maroon))) (complier, mcolor(blue) ciopts(color(blue))),  omitted  vertical keep(interaction1 interaction2 interaction3 zero_lead1  

interaction5 interaction6 interaction7 interaction8 interaction9 interaction10 interaction11 interaction12 interaction13 interaction14 interaction15 interaction16 interaction17 interaction18 interaction19 interaction20 interaction21)



title("`var'")




xlabel(1 "-4" 2 " " 3 "-2" 4 " " 5 "0" 6 " " 7 "2" 8 " " 9 "4" 10 " " 11 "6" 12 " " 13 "8" 

14 " " 15 "10" 16 " " 17 "12" 18 " " 19 "14" 20 " " 21 "16", angle(0) nogrid labsize(medsmall))



ylabel(0 "0" 0.05 "5%" 0.1 "10%" 0.15 "15%" 0.2 "20%" 0.25 "25%" ,nogrid angle(0) labsize(medsmall))


yscale(titlegap(*-3))


yline(0, lcolor(black))

xline(4, lcolor(black) lpattern(dash))

ytitle("Real Estate(y/n)", size(medium))

xtitle("Years", size(medsmall))

title("")


graphregion(fcolor(white)) bgcolor(white) scheme(s1color)

name(home, replace)

;


#delimit cr


graph export `var'_complierweight.pdf, replace

graph save `var'_complierweight, replace


drop outcome zero_lead1 pre post sumpre sumpost



}



* Secured debt


local depvar1 "mortgage_real"


foreach var of varlist `depvar1'{


gen outcome=`var'


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)

gen zero_lead1=0

xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo standard



xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* [pw=complier_weights_5] if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo complier


#delimit ; 


coefplot (standard, mcolor(maroon) ciopts(color(maroon))) (complier, mcolor(blue) ciopts(color(blue))),  omitted  vertical keep(interaction1 interaction2 interaction3 zero_lead1  

interaction5 interaction6 interaction7 interaction8 interaction9 interaction10 interaction11 interaction12 interaction13 interaction14 interaction15 interaction16 interaction17 interaction18 interaction19 interaction20 interaction21)



title("`var'")




xlabel(1 "-4" 2 " " 3 "-2" 4 " " 5 "0" 6 " " 7 "2" 8 " " 9 "4" 10 " " 11 "6" 12 " " 13 "8" 

14 " " 15 "10" 16 " " 17 "12" 18 " " 19 "14" 20 " " 21 "16", angle(0) nogrid labsize(medsmall))



ylabel(0 "0" 100000 "100" 200000 "200" 300000 "300",nogrid angle(0) labsize(medsmall))


yscale(titlegap(*-3))


yline(0, lcolor(black))

xline(4, lcolor(black) lpattern(dash))

ytitle("Secured Debt", size(medium))

xtitle("Years", size(medsmall))

title("")


graphregion(fcolor(white)) bgcolor(white) scheme(s1color)

name(mortgage, replace)

;


#delimit cr


graph export `var'_complierweight.pdf, replace

graph save `var'_complierweight, replace


drop outcome zero_lead1 pre post sumpre sumpost



}




* Unsecured debt

local depvar1 "bankgaeld_real"


foreach var of varlist `depvar1'{


gen outcome=`var'


replace outcome=. if time<-4

replace outcome=. if time>16


gen pre=0

gen post=0

replace pre=1 if (outcome~=. & time<1)

replace post=1 if (outcome~=. & time>0)


by pnr: egen sumpre=sum(pre)

by pnr: egen sumpost=sum(post)

gen zero_lead1=0

xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo standard



xtreg outcome interaction1 interaction2 interaction3 zero_lead1 interaction5-interaction21 yeardum2-yeardum33 timedum* [pw=complier_weights_5] if (time>-5 & time<17 & sumpre>0 & sumpost>0 ) , cluster(pnr) fe
eststo complier


#delimit ; 


coefplot (standard, mcolor(maroon) ciopts(color(maroon))) (complier, mcolor(blue) ciopts(color(blue))),  omitted  vertical keep(interaction1 interaction2 interaction3 zero_lead1  

interaction5 interaction6 interaction7 interaction8 interaction9 interaction10 interaction11 interaction12 interaction13 interaction14 interaction15 interaction16 interaction17 interaction18 interaction19 interaction20 interaction21)



title("`var'")




xlabel(1 "-4" 2 " " 3 "-2" 4 " " 5 "0" 6 " " 7 "2" 8 " " 9 "4" 10 " " 11 "6" 12 " " 13 "8" 

14 " " 15 "10" 16 " " 17 "12" 18 " " 19 "14" 20 " " 21 "16", angle(0) nogrid labsize(medsmall))



ylabel(0 "0" -100000 "-100" -200000 "-200" -300000 "-300",nogrid angle(0) labsize(medsmall))


yscale(titlegap(*-3))


yline(0, lcolor(black))

xline(4, lcolor(black) lpattern(dash))

ytitle("Unsecured Debt", size(medium))

xtitle("Years", size(medsmall))

title("")


graphregion(fcolor(white)) bgcolor(white) scheme(s1color)

name(bankgaeld, replace)

;


#delimit cr


graph export `var'_complierweight.pdf, replace

graph save `var'_complierweight, replace


drop outcome zero_lead1 pre post sumpre sumpost



}




/* Combine */




grc1leg earnings employment assets wealth bankgaeld mortgage, graphregion(fcolor(white)) scheme(s1color) cols(2) rows(3) imargin(small) legendfrom(earnings)


graph save event_panel_complier, replace

graph export event_panel_complier.pdf, replace
